package com.yidu.hbatsams.action.TrainingPlan.zjj;


import com.yidu.hbatsams.domain.*;
import com.yidu.hbatsams.service.AtsBaseSportService;
import com.yidu.hbatsams.service.emp.AtsEmpAthleteService;
import com.yidu.hbatsams.service.emp.AtsEmpCoachService;
import com.yidu.hbatsams.service.TrainingPlan.AtsEmpAthleteSchemeService;
import com.yidu.hbatsams.service.TrainingPlan.zjj.PlanWeekService;
import com.yidu.hbatsams.utils.FileTools;
import com.yidu.hbatsams.utils.Message;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @author 周佳佳
 * @类的描述——
 * @date 2019/12/9下午 04:01
 */
@Controller
@RequestMapping("planWeekMapperController")
public class PlanWeekMapperController {
    @Resource
    PlanWeekService planWeekService;
    //文件上传管理工具
    @Resource
    FileTools fileTools;
    //教练表的注入
    @Resource
    AtsEmpCoachService atsEmpCoachService;
    //运动员接口
    @Resource
    AtsEmpAthleteService atsEmpAthleteService;
    //运动员和计划的接口
    @Resource
    AtsEmpAthleteSchemeService atsEmpAthleteSchemeService;

    @Resource
    AtsBaseSportService atsBaseSportService;

    @ResponseBody
    @RequestMapping("atsEmpCoach")
    public AtsEmpCoach findCoachexp(int coachId) {
        AtsEmpCoach atsEmpCoach = atsEmpCoachService.selectexpert(coachId);
        return atsEmpCoach;
    }

    @ResponseBody
    @RequestMapping("atsBaseSport")
    public AtsBaseSport selectByPrimaryKey(int sportId) {
        AtsBaseSport atsBaseSport = atsBaseSportService.selectByPrimaryKey(sportId);
        return atsBaseSport;
    }


    @ResponseBody
    @RequestMapping("findByIdpid")
    public List<AtsEmpAthleteScheme> findByIdpid(AtsEmpAthleteScheme atsEmpAthleteScheme) {

        List<AtsEmpAthleteScheme> list = atsEmpAthleteSchemeService.findByRemark(atsEmpAthleteScheme);
        return list;
    }

    @ResponseBody
    @RequestMapping("selectAthlete")
    public List<AtsEmpAthlete> selectAthlete(Integer coachId) {
        return atsEmpAthleteService.selectByCoachId(coachId);
    }
    @RequestMapping("addOrupdate")
    @ResponseBody
    public Message addOrupdate(AtsPlanWeek planWeek, String sportName, String type, HttpSession session){

        if("2".equals(type) && session.getAttribute("weekId")!=null){

            return new Message();//如果是点击的新增课计划进来并且已经新增了周计划，就不需要再增加一个周计划。
        }

        if(planWeek.getWeekId()==null) {
            planWeek.setWeekId((Integer) session.getAttribute("weekId"));
        }

        //修改成有效
        planWeek.setIsva(1);
        //传入体育项目名称
        planWeek.setItemName(sportName);
        //根据传过来教练id查名称
        AtsEmpCoach atsEmpCoach=atsEmpCoachService.findById(planWeek.getCoachId());
        //传入教练员名字
        planWeek.setCoachName(atsEmpCoach.getCoachName());
        Message message=new Message();
        if(planWeek.getFileone().getOriginalFilename()!=null &&!"".equals(planWeek.getFileone().getOriginalFilename())){
            try {
                String file1=fileTools.saveFile("TrainingPlan",planWeek.getFileone());
                planWeek.setFile1(file1);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(planWeek.getFiletwo().getOriginalFilename()!=null &&!"".equals(planWeek.getFiletwo().getOriginalFilename())){
            try {
                String file2 = fileTools.saveFile("TrainingPlan",planWeek.getFiletwo());
                planWeek.setFile2(file2);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(planWeek.getFilethree().getOriginalFilename()!=null &&!"".equals(planWeek.getFilethree().getOriginalFilename())){
            try {
                String file3=fileTools.saveFile("TrainingPlan",planWeek.getFilethree());
                planWeek.setFile3(file3);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(!StringUtils.isEmpty(planWeek.getWeekId())){
            AtsEmpAthleteScheme atsEmpAthleteScheme=new AtsEmpAthleteScheme();
            atsEmpAthleteScheme.setIsva(0);
            atsEmpAthleteScheme.setPid(planWeek.getWeekId());
            atsEmpAthleteScheme.setRemark("2");//周计划
            int rowsone=atsEmpAthleteSchemeService.updateRemark(atsEmpAthleteScheme);
            if(!StringUtils.isEmpty(planWeek.getAthleteId())){
                //new出计划教练员关联表
                AtsEmpAthleteScheme atsEmpAthlete=new AtsEmpAthleteScheme();
                atsEmpAthlete.setCreater(planWeek.getCreater());
                //传入2代表周训练计划
                atsEmpAthlete.setRemark("2");
                atsEmpAthlete.setPname(planWeek.getWeekName());
                atsEmpAthlete.setPid(planWeek.getWeekId());
                boolean flag=planWeek.getAthleteId().contains(",");
                if(flag==true) {
                    String[] athleteId = planWeek.getAthleteId().split(",");
                    String[] athleteName = planWeek.getAthleteName().split(",");
                    for (int i = 0; i < athleteId.length; i++) {
                        String athleteIdone = athleteId[i];
                        String athleteNameone = athleteName[i];
                        atsEmpAthlete.setAthleteId(Integer.valueOf(athleteIdone));
                        atsEmpAthlete.setAthleteName(athleteNameone);
                        atsEmpAthleteSchemeService.insertSelective(atsEmpAthlete);
                    }
                }else{
                    atsEmpAthlete.setAthleteId(Integer.valueOf(planWeek.getAthleteId()));
                    atsEmpAthlete.setAthleteName(planWeek.getAthleteName());
                    atsEmpAthleteSchemeService.insertSelective(atsEmpAthlete);
                }

                planWeekService.updateByPrimaryKeySelectiveFour(planWeek);
            }

        }else{
            planWeekService.insertSelectiveFour(planWeek);
        }

        Integer weekId=planWeek.getWeekId();
        session.setAttribute("weekId",weekId);

        message.setNum(1);
        message.setMsg("操作成功!!!");
        return message;
    }
    @RequestMapping("findById")
    @ResponseBody
    public AtsPlanWeek findById(Integer weekId,HttpSession session){

        AtsPlanWeek planWeek=planWeekService.findByIdFour(weekId);
        return planWeek;
    }
    @RequestMapping("countIsvaTwo")
    @ResponseBody
    public Message countIsvaTwo(Integer coachId){
        Message message=new Message();
        int rows=planWeekService.countIsvaTwo(coachId);
        if(rows>0){
            message.setNum(1);
        }else{
            message.setNum(0);
        }
        return message;
    }
    @RequestMapping("selectMaxIsvaTwo")
    @ResponseBody
    public int selectMaxIsvaTwo(Integer coachId){
        int max=planWeekService.selectMaxIsvaTwo(coachId);
        return max;
    }
    @RequestMapping("selectIsExists")
    @ResponseBody
    public Message selectIsExists(AtsPlanWeek atsPlanWeek){

        Message message  = new Message();
       boolean flag = planWeekService.selectIsExists(atsPlanWeek) ;

        if(flag){
            message.setNum(1);
            message.setMsg("下周计划已经生成，请点击编辑按钮");
        }else{
            message.setNum(0);
        }
        return message;
    }

}
